package com.cake.ssm.controller;

import com.cake.ssm.po.Coupon;
import com.cake.ssm.service.CouponService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by lijiawang1 on 2017/4/23.
 */
@SessionAttributes("AllCouponInfo")
@Controller
public class CouponController {

    @Resource
    private CouponService couponService;

    @RequestMapping("/insertCoupon")
    public String InsertCoupon(Coupon coupon)throws Exception
    {
        UUID uuid = UUID.randomUUID();

        Date date=new Date();
        SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         //String time=from.format(date);
         coupon.setCouponid(uuid.toString());
        // System.out.println("Controller:"+coupon);
         couponService.InsertCoupon(coupon);
         return "success";
    }
    @RequestMapping(value = "/SelectAllCoupon")
    public String SelectAllCoupon(Map<String,Object> map)throws Exception
    {
        List<Coupon> list=couponService.SelectAllCoupon();
        map.put("AllCouponInfo",list);
        System.out.println("collection======"+list);
        return "ShowCoupon";
    }
    @RequestMapping(value = "/DeleteCouponInfo{id}")
    public String DeleteCoupon(@RequestParam("id") String id)throws Exception
    {
        System.out.println("删除成功----CouponId="+id);
        couponService.DeleteCoupon(id);
        return  "success";
    }
    @ModelAttribute
    public void GetCoupon(@RequestParam(value="id",required=false) String id,Map<String,Object> map)throws Exception
    {
        if(id!=null) {
            Coupon coupon = couponService.SelectByCouponId(id);
            System.out.println("coupon======----======="+coupon);
            map.put("coupon",coupon);
        }
    }
    @RequestMapping(value = "/Update{id}")
    public String RedriectUpdateCoupon(@PathVariable("id") String id)throws Exception
    {
        return "UpdateCoupon";
    }

    @RequestMapping("/UpdateCouponInfo")
    public String UpdateCoupon(Coupon coupon)throws Exception
    {
        couponService.UpdateCoupon(coupon);
        System.out.println("修改成功！");
        return  "redirect:/SelectAllCoupon";
    }
}
